<?php

class app
{
    public function __construct($config = [])
    {
        if (!empty($config)){
            $_ENV['config'] = $config;
        }
        if (isset($_SERVER['PATH_INFO']) || isset($_SERVER['REDIRECT_URL'])) {
            $path = $_SERVER['PATH_INFO'] ?? $_SERVER['REDIRECT_URL'];
            $pathinfo = explode('/', $path);
        } else {
            $pathinfo = array('', 'index', 'index'); //默认的控制器和方法
        }
        $_ENV['controller'] = ucfirst($pathinfo[1]);
        $_ENV['action'] = $pathinfo[2];
    }

    public function run()
    {
        $this->load();
        $this->do_action();
    }
    public function load()
    {
        $files = require ROOT_PATH . 'config/loadClass.php';
        foreach ($files as $key => $value) {
            if (file_exists($value)) {
                include_once $value;
            }
        }
    }
    public function do_action()
    {
        include_once ROOT_PATH . 'app/controller/' . $_ENV['controller'] . '.php';
        $controller = "app\controller\\" . $_ENV['controller'];
        $obj = new $controller;
        call_user_func([$obj, $_ENV['action']]);
    }
}
